package com.thunbu.thunbu_meeting_system.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.thunbu.thunbu_meeting_system.R
import com.thunbu.thunbu_meeting_system.bean.DepartmentModel
import com.thunbu.thunbu_meeting_system.utils.TreeViewUtils
import com.thunbu.thunbu_meeting_system.view.treeView.NodeModel
import com.thunbu.thunbu_meeting_system.view.treeView.TreeNode
import com.thunbu.thunbu_meeting_system.view.treeView.TreeViewPopupView
import kotlinx.android.synthetic.main.include_inventory_edit.view.*

class SearchPop(context: Context,val callback:(Boolean,String?,String?,String?,String?,String?,String?,String?)->Unit):BottomPopupView(context) {
//    var typeNodes:MutableList<TreeNode<*>> = mutableListOf()
//    var typeNodeid:Long? = null
//    var userNodes:MutableList<TreeNode<*>> = mutableListOf()
//    var userNodeid:Long? = null
//    var departmentNodes:MutableList<TreeNode<*>> = mutableListOf()
//    var departmentNodeid:Long? = null
//    var addressNodes:MutableList<TreeNode<*>> = mutableListOf()
//    var addressNodeid:Long? = null
    var hasInit = false

    override fun getImplLayoutId(): Int {
        return R.layout.pop_search
    }

    override fun onCreate() {
//        initData()
        findViewById<View>(R.id.ll_reamark).visibility = View.GONE
        findViewById<View>(R.id.tv_shuliang_name).visibility = View.GONE
        findViewById<View>(R.id.et_shuliang).visibility = View.GONE
        val et_zcType = findViewById<EditText>(R.id.et_type).apply {
            isFocusable = true
            isFocusableInTouchMode = true
            isCursorVisible = true
            hint = "请输入"
        }
        val et_zcName = findViewById<EditText>(R.id.et_zc)
        val et_Brand = findViewById<EditText>(R.id.et_brand)
        val et_Model = findViewById<EditText>(R.id.et_model)
        val et_User = findViewById<EditText>(R.id.et_user).apply {
            isFocusable = true
            isFocusableInTouchMode = true
            isCursorVisible = true
            hint = "请输入"
        }
        val et_department = findViewById<EditText>(R.id.et_department).apply {
            isFocusable = true
            isFocusableInTouchMode = true
            isCursorVisible = true
            hint = "请输入"
        }
        val et_address = findViewById<EditText>(R.id.et_address).apply {
            isFocusable = true
            isFocusableInTouchMode = true
            isCursorVisible = true
            hint = "请输入"
        }
        findViewById<View>(R.id.iv_type_arraw).visibility = View.GONE
        findViewById<View>(R.id.iv_user_arraw).visibility = View.GONE
        findViewById<View>(R.id.iv_department_arraw).visibility = View.GONE
        findViewById<View>(R.id.iv_address_arraw).visibility = View.GONE

//        et_zcType.setOnClickListener {
//            showTreeViewPop(1,typeNodes,et_zcType)
//        }
//        et_User.setOnClickListener {
//            showTreeViewPop(2,userNodes,et_User)
//        }
//        et_department.setOnClickListener {
//            showTreeViewPop(3,departmentNodes,et_department)
//        }
//        et_address.setOnClickListener {
//            showTreeViewPop(4,addressNodes,et_address)
//        }
        //重置
        findViewById<View>(R.id.tv_left).setOnClickListener {
            dismiss()
            clean()
            callback(false,null,null,null,null,null,null,null)
        }
        //查询
        findViewById<View>(R.id.tv_right).setOnClickListener {
            dismiss()
            callback(true,et_type.text?.toString(),et_zcName.text?.toString(),et_Brand.text?.toString(),et_Model.text?.toString(),
                    et_User.text?.toString(),et_department.text?.toString(),et_address.text?.toString())
        }
        hasInit = true
    }

    fun clean() {
        if(!hasInit) return
        et_type.text.clear()
        et_zc.text.clear()
        et_brand.text.clear()
        et_model.text.clear()
        et_user.text.clear()
        et_department.text.clear()
        et_address.text.clear()
    }

//    private fun initData() {
//        TreeViewUtils.loadData(1,typeNodes)
//        TreeViewUtils.loadData(2,userNodes)
//        TreeViewUtils.loadData(3,departmentNodes)
//        TreeViewUtils.loadData(4,addressNodes)
//    }

//    private fun showTreeViewPop(type:Int,nodes:MutableList<TreeNode<*>>,view:EditText) {
//        if(nodes.isEmpty()){
//            ToastUtils.show("接口数据为空,请返回首页重新获取")
//            return
//        }
//        val treeViewPopupView = TreeViewPopupView(context)
//        treeViewPopupView.setData(nodes) {
//            //组织人员树的组织节点不做处理
//            if(type == 2 && it.content is DepartmentModel && !(it.content as DepartmentModel).isUser){
//                return@setData
//            }
//            treeViewPopupView.dismiss()
//            val treeNode = it as TreeNode<NodeModel>
//            when(type){
//                1 ->  {
//                    view.setText(treeNode.content.name)
//                    typeNodeid = treeNode.content.getmId()
//                }
//                2 ->  {
//                    view.setText(treeNode.content.name)
//                    userNodeid = treeNode.content.getmId()
//                }
//                3 ->  {
//                    view.setText(treeNode.content.name)
//                    departmentNodeid = treeNode.content.getmId()
//                }
//                4 ->  {
//                    view.setText(treeNode.content.name)
//                    addressNodeid = treeNode.content.getmId()
//                }
//            }
//
//        }
//        XPopup.Builder(context)
//                .asCustom(treeViewPopupView)
//                .show()
//    }

}